<!DOCTYPE HTML>
<html lang="zh_CN" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>ctype - Lithium 双引擎 v0.0.1</title>
        <!-- Custom HTML head -->
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="../favicon.svg">
        <link rel="shortcut icon" href="../favicon.png">
        <link rel="stylesheet" href="../css/variables.css">
        <link rel="stylesheet" href="../css/general.css">
        <link rel="stylesheet" href="../css/chrome.css">
        <link rel="stylesheet" href="../css/print.css" media="print">
        <!-- Fonts -->
        <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../fonts/fonts.css">
        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../highlight.css">
        <link rel="stylesheet" href="../tomorrow-night.css">
        <link rel="stylesheet" href="../ayu-highlight.css">

        <!-- Custom theme stylesheets -->
    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded "><a href="../intro.html"><strong aria-hidden="true">1.</strong> 简介</a></li><li class="chapter-item expanded "><a href="../Upgrade.html"><strong aria-hidden="true">2.</strong> 从单引擎迁移</a></li><li class="chapter-item expanded "><a href="../basic.html"><strong aria-hidden="true">3.</strong> 基本用法</a></li><li class="chapter-item expanded "><a href="../web/web.html"><strong aria-hidden="true">4.</strong> 网页接口</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../web/dev-tools.html"><strong aria-hidden="true">4.1.</strong> 开发者工具</a></li><li class="chapter-item expanded "><a href="../web/env.html"><strong aria-hidden="true">4.2.</strong> env</a></li><li class="chapter-item expanded "><a href="../web/exit.html"><strong aria-hidden="true">4.3.</strong> exit</a></li><li class="chapter-item expanded "><a href="../web/main-window.html"><strong aria-hidden="true">4.4.</strong> MainWindow 类</a></li><li class="chapter-item expanded "><a href="../web/open-window.html"><strong aria-hidden="true">4.5.</strong> 打开窗口</a></li><li class="chapter-item expanded "><a href="../web/plugins.html"><strong aria-hidden="true">4.6.</strong> 插件</a></li><li class="chapter-item expanded "><a href="../web/struct-type.html" class="active"><strong aria-hidden="true">4.7.</strong> ctype</a></li><li class="chapter-item expanded "><a href="../web/shell.html"><strong aria-hidden="true">4.8.</strong> 运行本地脚本</a></li><li class="chapter-item expanded "><a href="../web/tray-icon.html"><strong aria-hidden="true">4.9.</strong> 系统托盘</a></li></ol></li><li class="chapter-item expanded "><a href="../cmdline.html"><strong aria-hidden="true">5.</strong> 命令行参数</a></li><li class="chapter-item expanded "><a href="../runtime.html"><strong aria-hidden="true">6.</strong> 运行时</a></li><li class="chapter-item expanded "><a href="../crash-report.html"><strong aria-hidden="true">7.</strong> 崩溃报告</a></li><li class="chapter-item expanded "><a href="../transparent-window.html"><strong aria-hidden="true">8.</strong> 透明窗口</a></li><li class="chapter-item expanded "><a href="../preferences.html"><strong aria-hidden="true">9.</strong> Preferences 配置文件</a></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">Lithium 双引擎 v0.0.1</h1>

                    <div class="right-buttons">
                        <a href="../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>
                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>
                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="web-中使用-ctype"><a class="header" href="#web-中使用-ctype">Web 中使用 ctype</a></h1>
<blockquote>
<p>StructType(name, fields) =&gt; ctypes.StructType(name, ff)</p>
</blockquote>
<pre><code class="language-html">&lt;html manifest=&quot;test.manifest&quot;&gt;
  &lt;meta charset=&quot;utf-8&quot; /&gt;
  &lt;body
    contextmenu=&quot;test_menu&quot;
    style=&quot;background-color: white;font-size: 12px;&quot;
  &gt;
    &lt;menu type=&quot;context&quot; id=&quot;test_menu&quot;&gt;
      &lt;menuitem
        label=&quot;调试器&quot;
        onclick=&quot;lnks.DevTools.toggle(window)&quot;
      &gt;&lt;/menuitem&gt;
      &lt;menuitem label=&quot;刷新&quot; onclick=&quot;location.reload()&quot;&gt;&lt;/menuitem&gt;
    &lt;/menu&gt;
    &lt;pre id=&quot;info&quot;&gt;&lt;/pre&gt;
    &lt;script&gt;
      let ctypes = lnks.ctypes;

      //定义drm相关的数据类型，需要保证和/usr/include/xf86XX.h中保持一致。
      const drmVersion = lnks.StructType(&quot;drmVersion&quot;, [
        { version_major: &quot;ctypes.int&quot; },
        { version_minor: &quot;ctypes.int&quot; },
        { version_patchlevel: &quot;ctypes.int&quot; },

        { name_len: &quot;ctypes.int&quot; },
        { name: &quot;ctypes.char.ptr&quot; },

        { date_len: &quot;ctypes.int&quot; },
        { date: &quot;ctypes.char.ptr&quot; },

        { desc_len: &quot;ctypes.int&quot; },
        { desc: &quot;ctypes.char.ptr&quot; }
      ]);

      const drmModeModeInfo = lnks.StructType(&quot;drmModeModeInfo&quot;, [
        { clock: &quot;ctypes.uint32_t&quot; },

        { hdisplay: &quot;ctypes.uint16_t&quot; },
        { hsync_start: &quot;ctypes.uint16_t&quot; },
        { hsync_end: &quot;ctypes.uint16_t&quot; },
        { htotal: &quot;ctypes.uint16_t&quot; },
        { hskew: &quot;ctypes.uint16_t&quot; },

        { vdisplay: &quot;ctypes.uint16_t&quot; },
        { vsync_start: &quot;ctypes.uint16_t&quot; },
        { vsync_end: &quot;ctypes.uint16_t&quot; },
        { vtotal: &quot;ctypes.uint16_t&quot; },
        { vscan: &quot;ctypes.uint16_t&quot; },

        { vrefresh: &quot;ctypes.uint32_t&quot; },

        { flags: &quot;ctypes.uint32_t&quot; },
        { type: &quot;ctypes.uint32_t&quot; },

        { name: &quot;ctypes.char.ptr&quot; }
      ]);

      const drmModeConnector = lnks.StructType(&quot;drmModeConnector&quot;, [
        { connector_id: &quot;ctypes.uint32_t&quot; },
        { encoder_id: &quot;ctypes.uint32_t&quot; },
        { connector_type: &quot;ctypes.uint32_t&quot; },
        { connector_type_id: &quot;ctypes.uint32_t&quot; },

        { connection: &quot;ctypes.int&quot; },

        { mmWidth: &quot;ctypes.uint32_t&quot; },
        { mmHeight: &quot;ctypes.uint32_t&quot; },

        { drmModeSubPixel: &quot;ctypes.int&quot; },

        { count_modes: &quot;ctypes.int&quot; },
        { modes: &quot;ctypes.void_t.ptr&quot; },

        { count_props: &quot;ctypes.int&quot; },
        { props: &quot;ctypes.uint32_t.ptr&quot; },
        { props_values: &quot;ctypes.uint64_t.ptr&quot; },

        { count_encoders: &quot;ctypes.int&quot; },
        { encoders: &quot;ctypes.uint32_t.ptr&quot; }
      ]);

      const drmModeRes = lnks.StructType(&quot;drmModeRes&quot;, [
        { count_fbs: &quot;ctypes.int&quot; },
        { fbs: &quot;ctypes.PointerType(ctypes.uint32_t)&quot; },

        { count_crtcs: &quot;ctypes.int&quot; },
        { crtcs: &quot;ctypes.PointerType(ctypes.uint32_t)&quot; },

        { count_connectors: &quot;ctypes.int&quot; },
        { connectors: &quot;ctypes.PointerType(ctypes.uint32_t)&quot; },

        { count_encoders: &quot;ctypes.int&quot; },
        { encoders: &quot;ctypes.PointerType(ctypes.uint32_t)&quot; },

        { min_width: &quot;ctypes.uint32_t&quot; },
        { max_width: &quot;ctypes.uint32_t&quot; },
        { min_height: &quot;ctypes.uint32_t&quot; },
        { max_height: &quot;ctypes.uint32_t&quot; }
      ]);

      //打开libc.so
      var libc = ctypes.open(&quot;libc.so.6&quot;);

      //打开libdrm.so
      var libdrm = ctypes.open(&quot;libdrm.so&quot;);

      // 申明需要使用到的函数列表
      let libc_open = libc.declare(
        &quot;open&quot;,
        ctypes.default_abi,
        ctypes.int,
        ctypes.char.ptr,
        ctypes.int
      );

      let drmGetRenderDeviceNameFromFd = libdrm.declare(
        &quot;drmGetRenderDeviceNameFromFd&quot;,
        ctypes.default_abi,
        ctypes.char.ptr,
        ctypes.int
      );
      let drmModeGetResources = libdrm.declare(
        &quot;drmModeGetResources&quot;,
        ctypes.default_abi,
        drmModeRes.ptr,
        ctypes.int
      );
      let drmGetVersion = libdrm.declare(
        &quot;drmGetVersion&quot;,
        ctypes.default_abi,
        drmVersion.ptr,
        ctypes.int
      );
      let drmModeGetConnector = libdrm.declare(
        &quot;drmModeGetConnector&quot;,
        ctypes.default_abi,
        drmModeConnector.ptr,
        ctypes.int,
        ctypes.int
      );

      let INFO = {};

      //打开DRM设备，若有多张显卡需要调整路径。
      let fd = libc_open(&quot;/dev/dri/card0&quot;, 2); //O_RDWR

      function readString(v) {
        let stringVal = &quot;&quot;;
        if (v &amp;&amp; v.contents) {
          stringVal = v.readString();
        }
        return stringVal;
      }

      INFO[&quot;DRM_FD&quot;] = fd;
      INFO[&quot;渲染设备名称&quot;] = readString(drmGetRenderDeviceNameFromFd(fd));

      let ver = drmGetVersion(fd).contents;
      INFO[&quot;DRM版本名称&quot;] = readString(ver.name);
      INFO[&quot;DRM版本日期&quot;] = readString(ver.date);
      INFO[&quot;DRM厂商&quot;] = readString(ver.desc);

      //获取mode相关信息
      let allRes = drmModeGetResources(fd).contents;

      function toIntArray(v, len) {
        let r = [];
        let tmp = ctypes.cast(v, ctypes.uint32_t.array(len).ptr).contents;
        for (let i = 0; i &lt; len; i++) {
          r.push(tmp[i]);
        }
        return r;
      }
      let connectors = toIntArray(allRes.connectors, allRes.count_connectors);
      let crtcs = toIntArray(allRes.crtcs, allRes.count_crtcs);
      let encoders = toIntArray(allRes.encoders, allRes.count_encoders);
      INFO[&quot;连接器&quot;] = connectors;
      INFO[&quot;控制器&quot;] = crtcs;
      INFO[&quot;编码器&quot;] = encoders;

      //输出connector的详细信息。
      function dump_connector_info(conn) {
        INFO[&quot;当前使用的连接器ID:&quot;] = conn.connector_id;
        INFO[&quot;编码器ID&quot;] = conn.encoder_id;

        let mode = ctypes.cast(conn.modes, drmModeModeInfo.ptr).contents;
        INFO[&quot;物理分辨率&quot;] = {
          宽: mode.hdisplay,
          高: mode.vdisplay
        };
        INFO[&quot;物理尺寸&quot;] = {
          宽: conn.mmWidth,
          高: conn.mmHeight
        };
      }

      for (let i = 0; i &lt; allRes.count_connectors; i++) {
        let conn = drmModeGetConnector(fd, connectors[i]).contents;

        //当前连接上的connector且mode数量大于0。(也就是当前正在使用的显示器)
        if (conn.connection == 1 &amp;&amp; conn.count_modes &gt; 0) {
          dump_connector_info(conn);
        }
      }

      document.getElementById(&quot;info&quot;).innerText = JSON.stringify(
        INFO,
        null,
        &quot; &quot;
      );
      if (window.lnks) {
        win = new window.lnks.MainWindow(window);
        let width = 600;
        let height = 700;
        win.setSize(width, height);
      }
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

</code></pre>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../web/plugins.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                            <a rel="next" href="../web/shell.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../web/plugins.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                    <a rel="next" href="../web/shell.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>

        <script type="text/javascript">
            window.playground_copyable = true;
        </script>
        <script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
        <script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="../book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->
    </body>
</html>
